package master选举.zkclient实现;

import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * author Veng Su
 * email  1344114844@qq.com
 * date   18-9-17 下午4:25
 */
public class MasterSelectorTest {
    public final static String CONNECTSTRING = "192.168.0.201:2181,192.168.0.203:2181,192.168.0.204:2181";

    @Test
    public void testSelect(){
        List<MasterSelector> masterSelectors = new ArrayList<>();

        List<ZkClient> clients=new ArrayList<>();
        try {


            for (int i = 0; i < 10; i++) {
                ZkClient zkClient = new ZkClient(CONNECTSTRING, 5000, 5000, new SerializableSerializer());

                UserCenter server = new UserCenter();
                server.setMac_id(new Random().nextInt(100));
                server.setMac_name("客户端=" + server.getMac_id());
                MasterSelector masterSelector = new MasterSelector(server,zkClient);
                masterSelector.start();

                masterSelectors.add(masterSelector);
            }
            TimeUnit.SECONDS.sleep(50);
        }catch (Exception e){
//            e.printStackTrace();
        }finally {
            for (MasterSelector masterSelector:
                 masterSelectors) {
                masterSelector.stop();
            }
        }
    }
}
